package besta.moon.engine.store;

import java.sql.*;

import besta.moon.MoonConnection;
import besta.moon.engine.Table;
import besta.moon.engine.View;

public class TableStorePage extends MoonPage {

    final Table table;

    public int lockType;
    public MoonConnection con;
    public TableStorePage nextLock;

    public TableStorePage(MoonConnection con, Table table, int lockType, long fileOffset) {
        super(null, 0, table.raFile, fileOffset);
        this.con = con;
        this.table = table;
        this.lockType = lockType;
    }

    byte[] getData() {
        return page;
    }

    @Override
    public long commit() throws SQLException {
        if (nextLock != null) {
            fileOffset = nextLock.commit();
            nextLock = null;
            rollback();
            return fileOffset;
        }
        if (lockType == View.LOCK_READ) {
            return fileOffset;
        }
        return super.commit();
    }

    @Override
    public final void freeLock() {
        table.freeLock(this);
    }
}
